<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>往来收款及核销</title>
	<include file="Public:header" />
	<style type="text/css">
		.input{width:180px; border:1px solid #ccc; padding:2px;}
		.input0{border:0px; padding:0px; margin:0px; height:14px; overflow:hidden;}
		.input2{border:1px solid #ccc; padding:0px; margin:1px 0px 1px 0px; height:20px; display:inline-block;}
		.select{width:180px; border:1px solid #ccc; height:25px; padding:2px;}
	</style>
	<script type="text/javascript">		
		function chkfrm(action_flg)
		{
			
			if($('#acc_id').val()=='')
			{
				$.messager.show({title:'提示',msg:'请选择客户',timeout:5000,showType:'slide'});
				return false;
			}
			if($('#curr_code').val()=='')
			{
				$.messager.show({title:'提示',msg:'请选择货币',timeout:5000,showType:'slide'});
				return false;
			}
			if(action_flg==0)
			{
				$("#form1").attr("action","{:U('Finance/fa_checkin_add')}");
				$('#action_flg').attr('value','CALLBACK');
				$('#form1').submit();
				return;
			}
			var v_curr_dr_total = $('#curr_dr_total').val();
			var v_curr_open_total = $('#curr_open_total').val();
			var selChk=0;//遍历得到每个checkbox的value值			
			$(".checkbox_writeoff").each(function(){
				if(this.checked){/*selChk+=this.value+",";*/selChk+=1;}
			});
			
			if(isNaN(v_curr_dr_total) || isNaN(v_curr_open_total))
			{
				$.messager.show({title:'提示',msg:'收款金额或核销金额不正确',timeout:5000,showType:'slide'});
				return false;
			}
			if(parseFloat(v_curr_dr_total)==0 && parseFloat(v_curr_open_total)==0 && selChk==0)
			{
				$.messager.show({title:'提示',msg:'没有收款金额或要核销的明细',timeout:5000,showType:'slide'});
				return false;
			}
			if(parseFloat(v_curr_dr_total)<0)
			{
				$.messager.show({title:'提示',msg:'过量核销：收款金额小于0',timeout:5000,showType:'slide'});
				return false;
			}
			if(parseFloat(v_curr_open_total)<0)
			{
				$.messager.show({title:'提示',msg:'过量核销：核销金额小于0',timeout:5000,showType:'slide'});
				return false;
			}
			if(parseFloat(v_curr_open_total)>parseFloat(v_curr_dr_total))
			{
				$.messager.show({title:'提示',msg:'过量核销：核销金额大于收款金额',timeout:5000,showType:'slide'});
				return false;
			}
			
			
			
			$("#form1").attr("action","{:U('Finance/fa_checkin_add_save')}");
			$('#action_flg').attr('value','FULL');
			$('#form1').submit();
		}
		
		function open_facc_window(itemno)
		{
			open_popup_faccmas('objcode=checkitem['+itemno+'][acc_code]&objtitle=checkitem['+itemno+'][acc_title]');
		}
		function open_curr_window()
		{
			open_popup_currmas('objcode=curr_code&objrate=curr_rate&callback=parent.chkfrm(0);');
		}
		function open_supplier_window()
		{
			open_popup_supplier('objid=acc_id&objtitle=acc_title&callback=parent.chkfrm(0);');
		}
		function open_customer_window()
		{
			open_popup_customer('objid=acc_id&objtitle=acc_title&callback=parent.chkfrm(0);');
		}
		//检查并计算收款项
		function calc(itemno)
		{
			if($("input[id='checkitem["+itemno+"][acc_code]']").val()=="")
			{
				$("input[id='checkitem["+itemno+"][curr_dr]']").attr("value","");
				$("input[id='checkitem["+itemno+"][dr]']").attr("value","");
				return false;
			}
			var v_curr_rate = $("#curr_rate").val();
			var v_curr_dr = $("input[id='checkitem["+itemno+"][curr_dr]']").val();
			if(isNaN(v_curr_rate) || isNaN(v_curr_dr))
			{
				$("input[id='checkitem["+itemno+"][curr_dr]']").attr("value","");
				$("input[id='checkitem["+itemno+"][dr]']").attr("value","");
				v_curr_dr = "";
			}

			if(v_curr_dr==""){
				$("input[id='checkitem["+itemno+"][curr_dr]']").attr("value","");
				$("input[id='checkitem["+itemno+"][dr]']").attr("value","");
			}else{
				$("input[id='checkitem["+itemno+"][dr]']").attr("value",v_curr_dr*v_curr_rate);
			}
			calc_total();
		}
		function calc_total()
		{
			var v_item_num = $("#item_num").val();
			var v_total_curr_dr = 0;
			var v_total_dr = 0;
			
			for(itemp=0;itemp<v_item_num;itemp++)
			{
				if($('#datarow'+itemp).length<=0) continue;
				v_total_curr_dr += $("input[id='checkitem["+itemp+"][curr_dr]']").val()=="" ? 0 : parseFloat($("input[id='checkitem["+itemp+"][curr_dr]']").val());
				v_total_dr += $("input[id='checkitem["+itemp+"][dr]']").val()=="" ? 0 : parseFloat($("input[id='checkitem["+itemp+"][dr]']").val());
			}
			$("#curr_dr_total").attr("value",v_total_curr_dr.toFixed(2));
			$("#dr_total").attr("value",v_total_dr.toFixed(2));
		}
		
		function addRow(rownum)
		{
			var row_start = parseInt($("#item_num").val());
			var row_end = row_start + rownum;
			
			for(itemp=row_start;itemp<row_end;itemp++)
			{
				var list_html = '<div id="datarow'+itemp+'" class="list-body">	<div class="list-t-l w250px">';				
				list_html += '	<span class="searchbox input2" >';
				list_html += '	<input type="text" id="checkitem['+itemp+'][acc_code]" name="checkitem['+itemp+'][acc_code]" value="{$faccdefault.acc_code}" readonly="true" class="input0" style="width:45px; " />';
				list_html += '	<input id="checkitem['+itemp+'][acc_title]" name="checkitem['+itemp+'][acc_title]" value="{$faccdefault.acc_title}" readonly="true" type="text" class="input0" style="width:180px; " />';
				list_html += '	<span><span class="searchbox-button" onclick="open_facc_window('+itemp+')"></span></span></span>';
				list_html += '</div>';
				list_html += '<div class="list-t-l w150px"><input type="text" onchange="calc('+itemp+')" onkeyup="chk_amount_input(\'dr\','+itemp+')" id="checkitem['+itemp+'][curr_dr]" name="checkitem['+itemp+'][curr_dr]" class="input2" style="width:150px;" /></div>';
				list_html += '<div class="list-t-l w150px"><input type="text" onchange="calc('+itemp+')" onkeyup="chk_amount_input(\'dr\','+itemp+')" id="checkitem['+itemp+'][dr]" name="checkitem['+itemp+'][dr]" class="input2" style="width:147px;"  /></div>';
				list_html += '<div class="list-t-l w60px"><a href="javascript:;" onclick="datarow_remove('+itemp+')">删除行'+(itemp+1)+'</a></div>';
				list_html += '</div>';
				$("#facheck_list_body").append(list_html);
			}
			$("#item_num").attr("value",row_end);
		}
		function datarow_remove(itemno)
		{
			$.messager.confirm('询问', '确认要移除选中的行吗？', function(r){
				if (r){
						$('#datarow'+itemno).remove();
						calc_total();
					}
				});
		}
		function writeoff_change(itemno)
		{
			var v_curr_rate = $("input[id='vouitem["+itemno+"][curr_rate]']").val();
			var v_curr_dr = $("input[id='vouitem["+itemno+"][curr_dr]']").val();
			var v_curr_cr = $("input[id='vouitem["+itemno+"][curr_cr]']").val();
			var v_curr_open_amount = $("input[id='vouitem["+itemno+"][curr_open_amount]']").val();
			var v_allow_amount = v_curr_dr=="" ? v_curr_cr : v_curr_dr;
			//alert(v_curr_open_amount);return false;
			if(''==v_curr_rate || ''==v_allow_amount || ''==v_curr_open_amount)
			{
				$("input[id='vouitem["+itemno+"][curr_open_amount]']").attr("value","");
				$("input[id='vouitem["+itemno+"][open_amount]']").attr("value","");
				$("input[id='vouitem["+itemno+"][writeoff]']").removeAttr("checked");
				writeoff_total();return false;
			}
			if(isNaN(v_curr_rate) || isNaN(v_allow_amount) || isNaN(v_curr_open_amount))
			{
				$("input[id='vouitem["+itemno+"][curr_open_amount]']").attr("value","");
				$("input[id='vouitem["+itemno+"][open_amount]']").attr("value","");
				$("input[id='vouitem["+itemno+"][writeoff]']").removeAttr("checked");
				writeoff_total();return false;
			}
			else if(parseFloat(v_curr_open_amount)>parseFloat(v_allow_amount))
			{
				$("input[id='vouitem["+itemno+"][curr_open_amount]']").attr("value",v_allow_amount);
				$("input[id='vouitem["+itemno+"][open_amount]']").attr("value",parseFloat(v_allow_amount)*parseFloat(v_curr_rate));
				$("input[id='vouitem["+itemno+"][writeoff]']").attr("checked","true");
			}
			else
			{
				$("input[id='vouitem["+itemno+"][curr_open_amount]']").attr("value",v_curr_open_amount);
				$("input[id='vouitem["+itemno+"][open_amount]']").attr("value",parseFloat(v_curr_open_amount)*parseFloat(v_curr_rate));
				$("input[id='vouitem["+itemno+"][writeoff]']").attr("checked","true");
			}
			writeoff_total();
		}
		function writeoff_set(itemno)
		{
			if(!!$("input[id='vouitem["+itemno+"][writeoff]']").attr("checked"))
			{
				var v_curr_rate = $("input[id='vouitem["+itemno+"][curr_rate]']").val();
				var v_curr_dr = $("input[id='vouitem["+itemno+"][curr_dr]']").val();
				var v_curr_cr = $("input[id='vouitem["+itemno+"][curr_cr]']").val();
				var v_allow_amount = v_curr_dr=="" ? v_curr_cr : v_curr_dr;
				
				$("input[id='vouitem["+itemno+"][curr_open_amount]']").attr("value",v_allow_amount);
				$("input[id='vouitem["+itemno+"][open_amount]']").attr("value",parseFloat(v_allow_amount)*parseFloat(v_curr_rate));
				$("input[id='vouitem["+itemno+"][writeoff]']").attr("checked","true");
			}
			else
			{
				$("input[id='vouitem["+itemno+"][curr_open_amount]']").attr("value","");
				$("input[id='vouitem["+itemno+"][open_amount]']").attr("value","");
				$("input[id='vouitem["+itemno+"][writeoff]']").removeAttr("checked");
				writeoff_total();return false;
			}
			writeoff_total();
		}
		function writeoff_total()
		{
			var v_item_num = $("#writeoff_item_num").val();
			var v_curr_open_total = 0;
			var v_open_total = 0;
			
			for(itemp=0;itemp<v_item_num;itemp++)
			{
				if($('#mydata'+itemp).length<=0) continue;
				
				var v_curr_dr = $("input[id='vouitem["+itemp+"][curr_dr]']").val();
				var v_curr_cr = $("input[id='vouitem["+itemp+"][curr_cr]']").val();
				var v_amount_flg = v_curr_dr=="" ? -1 : 1;
			
				v_curr_open_total += $("input[id='vouitem["+itemp+"][curr_open_amount]']").val()=="" ? 0 : parseFloat($("input[id='vouitem["+itemp+"][curr_open_amount]']").val())*v_amount_flg;
				v_open_total += $("input[id='vouitem["+itemp+"][open_amount]']").val()=="" ? 0 : parseFloat($("input[id='vouitem["+itemp+"][open_amount]']").val())*v_amount_flg;
			}
			$("#curr_open_total").attr("value",v_curr_open_total.toFixed(2));
			$("#open_total").attr("value",v_open_total.toFixed(2));
		}
		window.onload=function()
		{
			
		}
	</script>
</head>
<body class="">

	<div class="demo-info space-out-10px">
		<div class="demo-tip icon-tip"></div>
		<div>往来收款及核销 ：(1)点击放大镜可以选择所需的内容</div>
	</div>
	<form id="form1" method="post" action="{:U('Finance/fa_checkin_add_save')}">
	<input type="hidden" id="action_flg" name="action_flg" class="input" value="CALLBACK" />
	<div class="space-out-10px">
		<div style="width:100%; height:25px;">
				<div class="list-t-l w60px">单据号码：</div>
				<div class="list-t-l w200px"><input type="text" id="vouno" name="vouno" class="input" value="自动编号" readonly="true" /></div>
				
				<div class="list-t-l w60px" align="right">客户：</div>
				<div class="list-t-l w200px">
					<input type="text" id="acc_id" name="acc_id" value="{$acc_info.acc_id}" style="display:none;" />
					<span class="searchbox input2" >
					<input id="acc_title" name="acc_title" value="{$acc_info.acc_title}" readonly="true" type="text" class="input0" style="width:170px; " />
					<span><span class="searchbox-button" onclick="open_customer_window()"></span></span></span>
				</div>
				
				<div class="list-t-l w100px" align="right">日期：</div>
				<div class="list-t-l w200px"><input type="text" id="fadate" name="fadate" class="easyui-datebox input"  editable="false" value="{$fadate}" /></div>
		</div>
		<div style="height:3px; clear:both;"></div>
		<div style="width:100%; height:25px;">
				<div class="list-t-l w60px">单据状态：</div>
				<div class="list-t-l w200px"><input type="hidden" id="status_flg" name="status_flg" value="A" /><input type="text" name="tmp_title" class="input" value="活动" readonly="true" /></div>
				
				<div class="list-t-l w60px" align="right">货币：</div>
				<div class="list-t-l w200px">
					<span class="searchbox input2" >					
					<input id="curr_code" name="curr_code" value="{$currmas.code}" readonly="true" type="text" class="input0" style="width: 85px; ">
					<input id="curr_rate" name="curr_rate" value="{$currmas.rate}" readonly="true" type="text" class="input0" style="width: 80px; ">
					<span><span class="searchbox-button" onclick="open_curr_window()"></span></span></span>					
				</div>
				
				<div class="list-t-l w100px" align="right">摘要：</div>
				<div class="list-t-l w200px"><input type="text" id="remark" name="remark" class="input" /></div>
		</div>
		
		
		<div class="list-box">
		<fieldset style="border:1px dashed #ff9966; padding:15px; min-height:50px; background: #FFFEE6;"><legend><h3>现收</h3></legend>
		
		<div class="list-title">
			<div class="list-t-l w250px">收款到(银行或现金)</div>
			<div class="list-t-l w150px">收款金额</div>
			<div class="list-t-l w150px">本币金额</div>
		</div>
		<input type="hidden" id="item_num" name="item_num" value="1" style="display:none;" />
		<div id="facheck_list_body">
		<php>for($vouitem=0;$vouitem<1;$vouitem++){</php>
		<div id="datarow{$vouitem}" class="list-body">
			<div class="list-t-l w250px">				
				<span class="searchbox input2" >
				<input type="text" id="checkitem[{$vouitem}][acc_code]" name="checkitem[{$vouitem}][acc_code]" value="{$faccdefault.acc_code}" readonly="true" class="input0" style="width:45px; " />
				<input id="checkitem[{$vouitem}][acc_title]" name="checkitem[{$vouitem}][acc_title]" value="{$faccdefault.acc_title}" readonly="true" type="text" class="input0" style="width:180px; " />
				<span><span class="searchbox-button" onclick="open_facc_window({$vouitem})"></span></span></span>
			</div>
			<div class="list-t-l w150px"><input type="text" onchange="calc({$vouitem})" id="checkitem[{$vouitem}][curr_dr]" name="checkitem[{$vouitem}][curr_dr]" class="input2" style="width:150px;" /></div>
			<div class="list-t-l w150px"><input type="text" onchange="calc({$vouitem})" id="checkitem[{$vouitem}][dr]" name="checkitem[{$vouitem}][dr]" class="input2" style="width:147px;"  /></div>
			<div class="list-t-l w60px"><a href="javascript:;" onclick="datarow_remove({$vouitem})">删除行{$vouitem+1}</a></div>
		</div>
		<php>}</php>
		</div>
		
		<div style="height:3px; clear:both;"></div>	
		
		<div class="list-title">
			<div class="list-t-l w150px">
				<a href="javascript:void(0)" id="mb3" class="easyui-menubutton" menu="#menu_appendRow" iconCls="icon-add">增加行</a>
				<div id="menu_appendRow" style="width:150px;">
					<div onclick="addRow(1)">增加一行</div>
					<div class="menu-sep"></div>
					<div onclick="addRow(3)">增加三行</div>
					<div onclick="addRow(5)">增加五行</div>
					<div onclick="addRow(10)">增加十行</div>
				</div>
			</div>
			<div class="list-t-r">
			<div class="list-t-l w50px"></div>
			<div class="list-t-l w100px" style="text-align:center;">收到原币总计</div>
			<div class="list-t-l w100px"><input type="text" id="curr_dr_total" name="curr_dr_total" class="input2" value="0" readonly="true" style="width:97px;"  /></div>
			<div class="list-t-l w100px" style="text-align:center;">本币总计</div>
			<div class="list-t-l w100px"><input type="text" id="dr_total" name="dr_total" class="input2" value="0" readonly="true" style="width:97px;"  /></div>
			</div>
		</div>
		
		</fieldset>
		<div style="height:5px; clear:both;"></div>	
		<fieldset style="border:1px dashed #ff9966; padding:15px; min-height:50px; background: #FFFEE6;"><legend><h3>核销明细</h3></legend>
		
		<div class="list-title">
			<div class="list-t-l w120px">会计科目</div>
			<div class="list-t-l w50px">币别</div>
			<div class="list-t-l w50px">汇率</div>
			<div class="list-t-l w60px">原币借方</div>
			<div class="list-t-l w60px">借方金额</div>
			<div class="list-t-l w60px">原币贷方</div>
			<div class="list-t-l w60px">贷方金额</div>
			
			<div class="list-t-l w150px">说明</div>
			
			<div class="list-t-l w60px">本次核销</div>
			<div class="list-t-l w60px">核销本币</div>
		</div>
		
		<div id="writeoff_list_body">
		<input type="hidden" id="writeoff_item_num" name="writeoff_item_num" value="{$writeoff_item_num}" style="display:none;" />
		<volist name="mydata_item" id="mydata_item" key="k">
		<div id="mydata{$k-1}" class="list-body">
			<div class="list-t-l w120px">
				<span class="searchbox input2" >
				<input type="text" id="vouitem[{$k-1}][acc_code]" name="vouitem[{$k-1}][acc_code]" value="{$mydata_item.acc_code}" readonly="true" class="input0" style="width:45px; " />				
				<input id="vouitem[{$k-1}][acc_title]" name="vouitem[{$k-1}][acc_title]" value="{$mydata_item.acc_title}"  alt="{$mydata_item.acc_code} {$mydata_item.acc_title}" title="{$mydata_item.acc_code} {$mydata_item.acc_title}" readonly="true" type="text" class="input0" style="width:75px; " />
				</span>
			</div>
			<div class="list-t-l w50px">				
				<input id="vouitem[{$k-1}][curr_code]" name="vouitem[{$k-1}][curr_code]" value="{$mydata_item.curr_code}" readonly="true" type="text" class="input2" style="width: 50px; ">
			</div>
			<div class="list-t-l w50px"><input type="text" id="vouitem[{$k-1}][curr_rate]" name="vouitem[{$k-1}][curr_rate]" value="{$mydata_item.curr_rate}" class="input2" readonly="true" style="width:50px;" /></div>
			<div class="list-t-l w60px"><input type="text" id="vouitem[{$k-1}][curr_dr]" name="vouitem[{$k-1}][curr_dr]" value="<if condition="$mydata_item['curr_dr'] gt 0">{$mydata_item.curr_open_amount|get_round=###,6,false}</if>" class="input2" readonly="true" style="width:60px;" /></div>
			<div class="list-t-l w60px"><input type="text" id="vouitem[{$k-1}][dr]" name="vouitem[{$k-1}][dr]" value="<if condition="$mydata_item['dr'] gt 0">{$mydata_item.open_amount|get_round=###,6,false}</if>" class="input2" readonly="true" style="width:60px;"  /></div>
			<div class="list-t-l w60px"><input type="text" id="vouitem[{$k-1}][curr_cr]" name="vouitem[{$k-1}][curr_cr]" value="<if condition="$mydata_item['curr_cr'] gt 0">{$mydata_item.curr_open_amount|get_round=###,6,false}</if>" class="input2" readonly="true" style="width:60px;" /></div>
			<div class="list-t-l w60px"><input type="text" id="vouitem[{$k-1}][cr]" name="vouitem[{$k-1}][cr]" value="<if condition="$mydata_item['cr'] gt 0">{$mydata_item.open_amount|get_round=###,6,false}</if>" class="input2" readonly="true" style="width:60px;"  /></div>
			
			<div class="list-t-l w150px"><input type="text" id="vouitem[{$k-1}][ref_remark]" name="vouitem[{$k-1}][ref_remark]" value="{$mydata_item.mas_remark}" alt="{$mydata_item.mas_remark}" title="{$mydata_item.mas_remark}" class="input2" readonly="true" style="width:140px;"  /></div>
			
			<div class="list-t-l w60px"><input type="text" onchange="writeoff_change({$k-1})" id="vouitem[{$k-1}][curr_open_amount]" name="vouitem[{$k-1}][curr_open_amount]" value="0" class="input2" style="width:60px;" /></div>
			<div class="list-t-l w70px"><input type="text" onchange="writeoff_change({$k-1})" id="vouitem[{$k-1}][open_amount]" name="vouitem[{$k-1}][open_amount]" value="0" class="input2" style="width:60px;"  /></div>			
			<div class="list-t-l w60px">
				<input type="hidden" id="vouitem[{$k-1}][detail_id]" name="vouitem[{$k-1}][detail_id]" value="{$mydata_item.id}" />
				<input type="checkbox" id="vouitem[{$k-1}][writeoff]" name="vouitem[{$k-1}][writeoff]" value="Y" onclick="writeoff_set({$k-1})" class="checkbox_writeoff" />
				<label for="vouitem[{$k-1}][writeoff]">核销</label>
			</div>

		</div>
		</volist>
		</div>
		
		<div style="height:5px; clear:both;"></div>	
		
		<div class="list-title">
			<div class="list-t-l w250px"></div>
			<div class="list-t-r">
			<div class="list-t-l w50px"></div>
			<div class="list-t-l w100px" style="text-align:center;">核销原币总计</div>
			<div class="list-t-l w100px"><input type="text" id="curr_open_total" name="curr_open_total" class="input2" value="0" readonly="true" style="width:97px;"  /></div>
			<div class="list-t-l w100px" style="text-align:center;">本币总计</div>
			<div class="list-t-l w100px"><input type="text" id="open_total" name="open_total" class="input2" value="0" readonly="true" style="width:97px;"  /></div>
			</div>
		</div>
		
		</fieldset>
		</div>		
		
		
		<div style="height:10px; clear:both;"></div>		
	
		<div style="clear:both;">
				<div class="list-t-l w60px">制单：</div>
				<div class="list-t-l w200px"><input type="text" name="tmp_title" class="input" value="{$username}" readonly="true" /></div>
				<div class="list-t-l w100px" align="right">过账：</div>
				<div class="list-t-l w200px"><input type="text" name="tmp_title" class="input" value="未过账" readonly="true" /></div>
		</div>
		
		<div style="clear:both;">&nbsp;</div>
	
		<div style="clear:both;">
				<div class="list-t-l w60px">&nbsp;</div>
				<div>
					<input type="hidden" id="mas_code" name="mas_code" class="input" value="ARCHKIN" />
					<input type="hidden" id="manager" name="manager" class="input" value="0" />
					<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-save" onclick="chkfrm(1)">保存</a>
					<a href="{:U('Finance/fa_checkin_list')}" class="easyui-linkbutton" iconCls="icon-undo">取消</a>
				</div>
		</div>
	</div>
	</form>
	
	<div style="height:10px; clear:both;"></div>
	<include file="Public:window_popup" />
	
</body>
</html>
